/*
 * @Descripttion: 
 * @Author: 夜雨
 * @Email: 448408740@qq.com
 * @Date: 2019-12-02 14:30:53
 * @LastEditTime: 2019-12-11 11:28:55
 */
(function(){
	var mt = {};

	mt.cookie = {};

	mt.cookie.set = function(cname,cvalue,exdays){
		var d = new Date();
		d.setTime(d.getTime()+(exdays*24*60*60*1000));
		var expires = "expires="+d.toGMTString();
		var hostname = window.location.hostname;
		var domain = "domain="+hostname.substr(hostname.indexOf('.'));
		document.cookie = cname + "=" + cvalue + "; " + expires + "; " + domain + "; path=/";
	};

	mt.cookie.get = function(cname){
		var name = cname + "=";
		var ca = document.cookie.split(';');
		for(var i=0; i<ca.length; i++) 
		{
			var c = ca[i].trim();
			if (c.indexOf(name)==0) return c.substring(name.length,c.length);
		}
		return "";
	};

	(function(){
		var referrer = document.referrer;
		var origin = window.location.origin;
		if(referrer != '' && referrer.indexOf(origin) == -1){
			var href =  window.location.href;
			var track = encodeURIComponent(referrer) + '|' + encodeURIComponent(href);
			mt.cookie.set('US_TRACK', track);
		}
	})();
	
	(function(){
		var forms = document.querySelectorAll('form');
		for(var i=0;i<forms.length;i++){
			var submit =  forms[i].querySelector('input[type="submit"],button[type="submit"],input[type="image"]');
			submit.addEventListener('click', function(){
				var form = this.parentElement;
				var uts = form.querySelectorAll('input[name="usertrack"]');
				for (var j=0;j<uts.length;j++) {
					uts[j].remove();
				}
				var input_ut = document.createElement('input');
				input_ut.name = 'usertrack';
				input_ut.type = 'hidden';
				input_ut.value = mt.cookie.get('US_TRACK');
				form.insertBefore(input_ut, this);
			});
		}
	})();
	

})();
